﻿using System;
using System.Collections.Generic;
using Core.Common;
using Poker;

namespace Games
{
    public interface IGame
    {
        /// <summary>
        /// 游戏次数
        /// </summary>
        int GameIndex { get; set; }
        /// <summary>
        /// 每玩家手牌数量
        /// </summary>
        int MaxPokerCardCountPerPlayer { get; set; }
        /// <summary>
        /// 洗牌次数
        /// </summary>
        int ShuffleCount { get; set; }
        /// <summary>
        /// 游戏规则
        /// </summary>
        GameRule Rule { get; set; }
        /// <summary>
        /// 扑克牌清单
        /// </summary>
        Stack<PokerCard> PokerCards { get; set; }
        /// <summary>
        /// 玩家清单
        /// </summary>
        ICollection<Player> Players { get; set; }
        /// <summary>
        /// 发牌
        /// </summary>
        public void Deal()
        {
            Console.WriteLine("发牌!");
            for (int index = 0; index < MaxPokerCardCountPerPlayer; index++)
            {
                foreach (var player in Players)
                {
                    player.DeckOfCard.Add(PokerCards.Pop());
                }
            }
        }
        /// <summary>
        /// 洗牌
        /// </summary>
        /// <param name="count"></param>
        public void Shuffle()
        {
            Console.WriteLine("洗牌!");
            var count = ShuffleCount;
            if (count <= AppSettings.UNIT_ONE)
            {
                count = AppSettings.UNIT_ONE;
            }

            var pokerCards = PokerCards.ToShuffle();
            if (count >= AppSettings.UNIT_TWO)
            {
                for (int index = AppSettings.UNIT_ONE; index < count; index++)
                {
                    pokerCards = pokerCards.ToShuffle();
                }
            }

            PokerCards.Clear();
            foreach (var pokerCard in pokerCards)
            {
                PokerCards.Push(pokerCard);
            }
        }
        /// <summary>
        /// 玩一局游戏
        /// </summary>
        void Play();
        /// <summary>
        /// 比较结果
        /// </summary>
        /// <returns></returns>
        void Compare();
        /// <summary>
        /// 回收扑克
        /// </summary>
        public void Recycle()
        {
            Console.WriteLine("回收扑克!");
            if (Players.IsNotNullOrEmpty())
            {
                foreach (var player in Players)
                {
                    if (player.DeckOfCard.IsNotNullOrEmpty())
                    {
                        foreach (var pokerCard in player.DeckOfCard)
                        {
                            PokerCards.Push(pokerCard);
                        }

                        player.DeckOfCard.Clear();
                    }
                }
            }
        }
        /// <summary>
        /// 游戏结束
        /// </summary>
        public void Over()
        {
            if (PokerCards.IsNotNullOrEmpty())
            {
                PokerCards.Clear();
                Console.WriteLine("扑克牌已回收!");
            }

            Console.WriteLine("游戏结束!");
        }
    }
}
